using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using GenericSorter = Peradigm.Framework.Domain.Collections.Generic;
using Peradigm.Framework.Domain.Collections;
using Peradigm.Framework.Domain.Parties;
using Peradigm.Framework.UI.Web;

using Liberty.Domain.ServiceMemberManagement;
using Liberty.Application.ServiceMemberManagement;
using Liberty.Application.CaseManagement;

namespace Liberty.UI.Service.ServiceMemberManagement.Controls
{
    public partial class ServiceMemberListView : AbstractControl
    {
        #region Constants
        private const string
            HTML_CITY_LABEL_ID = "lblCity",
            HTML_STATE_LABEL_ID = "lblState",
            SESSION_SORT_DESCRIPTION = "SearchMemberListSortDescription",
            DEFAULT_SORT_PROPERTY = "Name";
            
        #endregion

        #region Event Handlers
        protected void Page_Load( object sender, EventArgs e )
        {
            if( !this.IsPostBack )
            {
                if (Profile.NumRows == 0) Profile.NumRows = 10;
                this.SortDescription = new SortDescription( DEFAULT_SORT_PROPERTY );                
                this.ddlNumRows.Items.FindByValue(Profile.NumRows.ToString()).Selected = true;
            }
        }

        protected void gvServiceMemberList_Sorting( object sender, GridViewSortEventArgs e )
        {
            this.SortDescription.Property = e.SortExpression;
        }

        protected void gvServiceMemberList_RowCommand( object sender, GridViewCommandEventArgs e )
        {
            switch (e.CommandName.ToString())
            {
                case "Name":
                    long oid = long.Parse(e.CommandArgument.ToString());
                    this.RaiseServiceMemberSelectedEvent(this.ServiceMemberWith(oid));
                    break;

                case "AddNewCase":
                    ServiceMember servicemember = this.ServiceMemberWith(Convert.ToInt64(e.CommandArgument));
                    this.RaiseAddNewCaseEvent(servicemember);
                    break;
            }

        }

        #region Events
        protected void ddlNumRows_SelectedIndexChanged(object sender, EventArgs e)
        {
           Profile.NumRows = Convert.ToInt32(ddlNumRows.SelectedValue);
        }

        #endregion

        protected void gvServiceMemberList_PageIndexChanging( object sender, GridViewPageEventArgs e )
        {
            gvServiceMemberList.DataSource = this.SortedServiceMembers();
            gvServiceMemberList.PageIndex = e.NewPageIndex;
            gvServiceMemberList.DataBind();
        }
       
        #endregion

        #region Methods
        public override void UpdateView()
        {
            gvServiceMemberList.PageSize = 25;
            base.UpdateView();
            if( this.ServiceMemberList != null && this.ServiceMemberList.Count > 0 )
            {
                lblNoResults.Visible = false;
                pnlServiceMemberList.Visible = true;
                gvServiceMemberList.DataSource = this.SortedServiceMembers() ;
                gvServiceMemberList.DataBind();
               
            }
            else
            {
                lblNoResults.Visible = true;
                pnlServiceMemberList.Visible = false;
            }
        }
        #endregion

        #region Properties
        public event ServiceMemberSelectedEventHandler ServiceMemberSelectedEventListerners
        {
            add
            {
                i_ServiceMemberSelectedEvent += value;
            }
            remove
            {
                i_ServiceMemberSelectedEvent -= value;
            }
        }

        public event AddNewCaseEventHandler AddNewCaseEventListerners
        {
            add
            {
                i_AddNewCaseEvent += value;
            }
            remove
            {
                i_AddNewCaseEvent -= value;
            }
        }
        #endregion

        #region Private Methods
        protected void RaiseServiceMemberSelectedEvent( ServiceMember serviceMember )
        {
            if( this.ServiceMemberSelectedEvent != null )
            {
                this.ServiceMemberSelectedEvent( serviceMember );
            }
        }

        private void RaiseAddNewCaseEvent(ServiceMember serviceMember)
        {
            if (this.AddNewCaseEvent != null)
            {
                this.AddNewCaseEvent(serviceMember);
            }
        }

        private ServiceMember ServiceMemberWith( long oid )
        {
            List<ServiceMember> serviceMembers = (List<ServiceMember>)this.Model;
            ServiceMember foundServiceMember = serviceMembers.Find( serviceMember => oid == serviceMember.Oid );
            return foundServiceMember;
        }

        private List<ServiceMember> SortedServiceMembers()
        {
            this.ServiceMemberList.Sort( new GenericSorter.Sorter<ServiceMember>( this.SortDescription ) );
            return this.ServiceMemberList;
        }
        #endregion

        #region Private Properties
        protected ServiceMemberSelectedEventHandler ServiceMemberSelectedEvent
        {
            get
            {
                return i_ServiceMemberSelectedEvent;
            }
        }

        private AddNewCaseEventHandler AddNewCaseEvent
        {
            get
            {
                return i_AddNewCaseEvent;
            }
            set
            {
                i_AddNewCaseEvent = value;
            }
        }

        private List<ServiceMember> ServiceMemberList
        {
            get
            {
                return (List<ServiceMember>)this.Model;
            }
        }

        private SortDescription SortDescription
        {
            get
            {
                return (SortDescription)this.Session[SESSION_SORT_DESCRIPTION];
            }
            set
            {
                this.Session[SESSION_SORT_DESCRIPTION] = value;
            }
        }
        #endregion

        #region Construction and Finalization
        public ServiceMemberListView()
            : base()
        {
        }
        #endregion

        #region Data Elements
        private ServiceMemberSelectedEventHandler i_ServiceMemberSelectedEvent;
        private AddNewCaseEventHandler i_AddNewCaseEvent;    
        #endregion




   
}
}
